package com.huayi.order.controller;

import com.huayi.order.enums.EnumErpOrderStatus;
import com.huayi.order.service.ErpSalesOrderService;
import com.huayi.order.service.ExpressCompanyService;
import com.huayi.order.utils.EnumResultVo;
import com.huayi.order.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/ajax_order")
public class AjaxOrderController {
    @Autowired
    private ErpSalesOrderService salesOrderService;
    @Autowired
    private ExpressCompanyService expressCompanyService;

    @RequestMapping(value = "/order_send", method = RequestMethod.POST)
    public ResultVo<Integer> confirmOrderPiPi(@RequestBody OrderSendReq req) {
        if (req.getOrderId() == null || req.getOrderId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError, "参数错误，缺少orderId");
        if (req.getLogisticsType() == null || req.getLogisticsType() < 0)
            return new ResultVo<>(EnumResultVo.ParamsError, "参数错误，缺少LogisticsType");

        if(req.getLogisticsType() == 0){
            //快递发货
            if(StringUtils.isEmpty(req.getContactPerson())||StringUtils.isEmpty(req.getMobile())||StringUtils.isEmpty(req.getAddress()))
                return new ResultVo<>(EnumResultVo.ParamsError, "参数错误，收货信息不完整");
            if(StringUtils.isEmpty(req.getLogisticsCompanyCode())||StringUtils.isEmpty(req.getLogisticsCode()))
                return new ResultVo<>(EnumResultVo.ParamsError, "参数错误，快递信息不完整");

            String company = expressCompanyService.getNameByCode(req.getLogisticsCompanyCode());
            req.setLogisticsCompany(company);

        }else{
            //用户自提
        }


        ResultVo<Integer> result = salesOrderService.sendOrder(req);

        return result;
    }

    /**
     * 取消订单
     * @param req
     * @return
     */
    @RequestMapping(value = "/order_cancel", method = RequestMethod.POST)
    public ResultVo<Integer> cancelOrderPiPi(@RequestBody OrderSendReq req) {
        if (req.getOrderId() == null || req.getOrderId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError, "参数错误，缺少orderId");

        var order = salesOrderService.getDetailById(req.getOrderId());
        if(order.getStatus().intValue() != EnumErpOrderStatus.WaitSend.getIndex()){
            return new ResultVo<>(EnumResultVo.ParamsError, "订单不能取消，订单状态："+EnumErpOrderStatus.getName(order.getStatus()));
        }

        ResultVo<Integer> resultVo =  salesOrderService.cancelOrder(req.getOrderId());

        return resultVo;
    }

    /**
     * 退款退货
     * @param req
     * @return
     */
    @RequestMapping(value = "/order_refund", method = RequestMethod.POST)
    public ResultVo<Integer> orderRefund(@RequestBody OrderSendReq req) {
        if (req.getOrderId() == null || req.getOrderId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError, "参数错误，缺少orderId");

        var order = salesOrderService.getDetailById(req.getOrderId());
        if(order.getStatus().intValue() != EnumErpOrderStatus.Delivered.getIndex()){
            return new ResultVo<>(EnumResultVo.ParamsError, "订单不能退货，订单状态："+EnumErpOrderStatus.getName(order.getStatus()));
        }

        ResultVo<Integer> resultVo =  salesOrderService.refundOrder(req.getOrderId());

        return resultVo;
    }

}
